import { userLogin, logout } from 'api/login';
import { getRole } from 'api/system';
import Cookies from 'js-cookie';

const user = {
  state: {
    // countType: Cookies.get('countType'),
    // lastlogin: Cookies.get('lastlogin'),
    // loginaddress: Cookies.get('loginaddress'),
    roleid: Cookies.get('roleid'),
    uid: Cookies.get('uid'),
    // usercreatedate: Cookies.get('usercreatedate'),
    username: Cookies.get('username'),
    // userpassword: Cookies.get('userpassword'),
    // userphone: Cookies.get('userphone'),
    // userstatus: Cookies.get('userstatus'),
    ownership: [],
    lastoptime: Cookies.get('lastoptime'),
    lastlogincode: Cookies.get('lastlogincode')
  },

  mutations: {
    // SET_COUNTYPE: (state, countType) => {
    //   Cookies.set('countType', countType);
    //   state.countType = countType;
    // },
    // SET_LASTLOGIN: (state, lastlogin) => {
    //   Cookies.set('lastlogin', lastlogin);
    // },
    // SET_LOGINADDRESS: (state, loginaddress) => {
    //   Cookies.set('loginaddress', loginaddress);
    //   state.loginaddress = loginaddress;
    // },
    SET_ROLEID: (state, roleid) => {
      Cookies.set('roleid', roleid);
      state.roleid = roleid;
    },
    SET_UID: (state, uid) => {
      Cookies.set('uid', uid);
      state.uid = uid;
    },
    // SET_USERCREATEDATE: (state, usercreatedate) => {
    //   Cookies.set('usercreatedate', usercreatedate);
    //   state.usercreatedate = usercreatedate;
    // },
    SET_USERNAME: (state, username) => {
      Cookies.set('username', username);
      state.username = username;
    },
    // SET_USERPASSWORD: (state, userpassword) => {
    //   Cookies.set('userpassword', userpassword);
    //   state.userpassword = userpassword;
    // },
    // SET_USERPHONE: (state, userphone) => {
    //   Cookies.set('userphone', userphone);
    //   state.userphone = userphone;
    // },
    // SET_USERSTATUS: (state, userstatus) => {
    //   Cookies.set('userstatus', userstatus);
    //   state.userstatus = userstatus;
    // },
    SET_OWNERSHIP: (state, ownership) => {
      Cookies.set('ownership', ownership);
      state.ownership = ownership
    },
    SET_LASTOPTIME: (state, lastoptime) => {
      Cookies.set('lastoptime', lastoptime);
      state.lastoptime = lastoptime
    },
    SET_LASTLOGINCODE: (state, lastlogincode) => {
      Cookies.set('lastlogincode', lastlogincode);
      state.lastlogincode = lastlogincode
    }
  },
  actions: {
    // 邮箱登录
    LoginByEmail({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        userLogin(userInfo).then(response => {
          const res = response.data;
          if (res.flag === 1) {
            const resdata = res.data[0];
            if (resdata) {
              commit('SET_UID', resdata.uid);
              commit('SET_USERNAME', resdata.username);
              // commit('SET_USERPASSWORD', resdata.userpassword);
              commit('SET_ROLEID', resdata.roleid);
              // commit('SET_LOGINADDRESS', resdata.loginaddress);
              // commit('SET_COUNTYPE', resdata.countType);
              // commit('SET_LASTLOGIN', resdata.lastlogin);
              commit('SET_OWNERSHIP', []);
              commit('SET_LASTOPTIME', new Date());
              commit('SET_LASTLOGINCODE', resdata.lastlogincode);
            }
            resolve();
          } else {
            resolve(res.message);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      const requestParams = {
        roleid: state.roleid,
        operatorid: state.uid
      }
      return new Promise((resolve, reject) => {
        getRole(requestParams).then(response => {
          const res = response.data;
          if (res.flag === 1) {
            const resdata = res.data;
            if (resdata.ownPermissionSoleCode) {
              const permissids = resdata.ownPermissionSoleCode.split(',');
              commit('SET_OWNERSHIP', permissids);
            } else {
              commit('SET_OWNERSHIP', ['']);
            }
          }
          resolve(response);
        }).catch(error => {
          reject(error);
        });
      });
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.uid).then(() => {
          commit('SET_UID', null);
          commit('SET_USERNAME', '');
          // commit('SET_USERPASSWORD', '');
          commit('SET_ROLEID', null);
          commit('SET_OWNERSHIP', []);
          Cookies.remove('uid');
          Cookies.remove('username');
          // Cookies.remove('userpassword');
          Cookies.remove('roleid');
          Cookies.remove('ownership');
          resolve();
        }).catch(error => {
          reject(error);
        });
      });
    },

    // 设置当前操作时间
    UpdateOptime: ({ commit }, lastoptime) => {
      commit('SET_LASTOPTIME', lastoptime)
    }
  }
};

export default user;
